package com.pan.operation;

import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建一个对象
 * @author by panstark
 * @description
 * @notice
 * @date 2021/4/28
 */
@Slf4j
public class CreateMyself implements Cloneable, Serializable {

    String id;

    String name;

    int age;

    Date birthday;


    /**
     * 如果没有写任何构造器，
     * 则默认有一个空构造器
     */
    public  CreateMyself(){

    }

    public CreateMyself(String name){
        this.name=name;
    }

    /**
     * 注意：
     * 这里如果用了int，则没有办法调用反射执行这个构造器
     * @param name
     * @param age
     */
    public CreateMyself(String name,Integer age){
        this.name=name;
        this.age=age;
    }

    public void selfIntroduction(String message){

        log.info("hello, I am create by {},id:{},name:{},age:{},birthday:{}",message,id,name,age,birthday);

    }

    /**
     * 直接new一个对象
     */
    public void createObject1(){
        CreateMyself CreateMyself = new CreateMyself();
        CreateMyself.selfIntroduction("new CreateMyself()");
    }

    /**
     * 通过class创建一个对象
     */
    public void createObject2(){
        Class<CreateMyself> reflectClass = CreateMyself.class;
        CreateMyself CreateMyself = null;
        try {
            CreateMyself = reflectClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        CreateMyself.selfIntroduction("reflectClass.newInstance()");
    }


    /**
     * 通过无参构造器
     * 构造一个对象
     */
    public void createObject3(){
        Class<CreateMyself> reflectClass = CreateMyself.class;
        try {
            Constructor<CreateMyself> declaredConstructor = reflectClass.getDeclaredConstructor();
            CreateMyself CreateMyself = declaredConstructor.newInstance();
            CreateMyself.selfIntroduction("declaredConstructor.newInstance()");
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过有参构造器构造一个对象
     */
    public void createObject4(){
        Class<CreateMyself> reflectClass = CreateMyself.class;
        try {
            Constructor<CreateMyself> declaredConstructor = reflectClass.getDeclaredConstructor(String.class);
            CreateMyself CreateMyself = declaredConstructor.newInstance("Jon");
            CreateMyself.selfIntroduction("declaredConstructor.newInstance()");
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过多参构造器构造一个对象
     */
    public void createObject5(){
        Class<CreateMyself> reflectClass = CreateMyself.class;
        try {
            Constructor<CreateMyself> declaredConstructor = reflectClass.getDeclaredConstructor(String.class,Integer.class);
            CreateMyself CreateMyself = declaredConstructor.newInstance("Jon",10);
            CreateMyself.selfIntroduction("declaredConstructor.newInstance()");
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    /**
     * 通过clone创建一个新的对象
     */
    public void createObject6(){
        Class<CreateMyself> reflectClass = CreateMyself.class;
        try {
            Constructor<CreateMyself> declaredConstructor = reflectClass.getDeclaredConstructor(String.class,Integer.class);
            CreateMyself createMyself = declaredConstructor.newInstance("Jon",10);
            createMyself.selfIntroduction("declaredConstructor.newInstance()");
            Object clone = createMyself.clone();
            System.out.println(createMyself);
            System.out.println(clone);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CreateMyself reflectDemo = new CreateMyself();
//        reflectDemo.createObject1();
//        reflectDemo.createObject2();
//        reflectDemo.createObject3();
//        reflectDemo.createObject4();
//        reflectDemo.createObject5();
//        reflectDemo.createObject6();

        List<String> array = new ArrayList<>();
        array.add("aa");
        array.add("bb");
        array.add("01");

        //模拟 00 aa bb 01 02 03
        List<String> bytestr = new ArrayList<>();
        bytestr.add("00");
        bytestr.add("aa");
        bytestr.add("bb");
        bytestr.add("01");
        bytestr.add("02");
        bytestr.add("03");
        int index = reflectDemo.aabbIndex(bytestr,array);
        System.out.println("index:"+index);
    }

    public int aabbIndex(List<String> bytestr,List<String> array){

        int size = array.size();

        for (int i = 0; i < bytestr.size()- size; i++) {
            int index=0;
            int bytestrIndex = i;
            while(index<size){
                if(bytestr.get(bytestrIndex).equals(array.get(index))){
                    bytestrIndex++;
                    index++;
                }else{
                    break;
                }
            }
            if(index==size){
                return i;
            }
        }
        return -1;
    }


}
